package com.example.xdzx.controller;

import com.example.xdzx.pojo.LostItems;
import com.example.xdzx.service.ItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ItemsController {
    @Autowired
    ItemsService itemsService;

    /**
     * 返回排序好的json对象数组
     * @return
     */
    @PostMapping("/getLostList")
    public Map<String,Object> getLostList(@RequestBody LostItems lostItems){
        Map<String,Object> resultMap=new HashMap<>();
        try {
            List<LostItems> lostList = itemsService.getLostList(lostItems);
            resultMap.put("isSuccess",true);
            resultMap.put("list",lostList);
        } catch (Exception e) {
            resultMap.put("isSuccess",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 返回某用户发布的json对象数组
     */
    @GetMapping("/getMyRelease")
    public List<Map<String,Object>> getMyRelease(@RequestParam("userId")Long userId){
        return itemsService.getMyRelease(userId);
    }

    /**
     * 发布丢失物品信息
     */
    @RequestMapping("/release")
    public Map<String,Boolean> release(@RequestParam("userId")Long userId,
                                       @RequestParam("capsuleGoods") String capsuleGoods,
                                       @RequestParam("capsuleAddr")String capsuleAddr,
                                       @RequestParam("type")String type,
                                       @RequestParam("userPic") String userPic,
                                       @RequestParam("imageUrl") String imageUrl){
        Map<String,Boolean> map = new HashMap<>();
        Boolean res = itemsService.release(userId,capsuleGoods,capsuleAddr,type,userPic,imageUrl);
        map.put("isSuccess",res);
        return map;
    }

    /**
     * 发布丢失物品信息
     */
    @RequestMapping("/uploadImg")
    public Map<String,Object>  uploadImg(@RequestPart("img") MultipartFile file){
        return itemsService.uploadImg(file);
    }
}
